package com.boot.web.serviceImpl;

import com.boot.web.dao.RoleDao;
import com.boot.web.dao.RolePowerDao;
import com.boot.web.entity.Role;
import com.boot.web.entity.RolePower;
import com.boot.web.service.RoleService;
import com.boot.web.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.exception.BusinessException;
import com.boot.base.serviceImpl.BaseServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统角色信息业务层接口实现
 * @author 【zhouJie】
 * @date 2019-12-05 17:31:35
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RolePowerDao rolePowerDao;

    @Override
    public List<RoleVO> getRoleVoPageList(Map<String, Object> map) {
        List<RoleVO> roleVOList = roleDao.queryRoleVoPageList(map);
        if (roleVOList.size() > 0) {
            for (RoleVO roleVO : roleVOList) {
                List<RolePower> rpList = rolePowerDao.queryRolePowerByRoleId(roleVO.getId());
                if (rpList.size() > 0) {
                    roleVO.setRolePowerList(rpList);
                }
            }
        }
        return roleVOList;
    }

    public boolean judgeSignIsExist(String sign) {
        Role role = roleDao.queryRoleBySign(sign);
        if (role != null) {
            throw new BusinessException("角色[系统标识]已存在");
        }
        return true;
    }

    @Override
    public Role saveRole(Role role) {
        judgeSignIsExist(role.getSign());
        role.setUpdateTime(new Date());
        role.setDefaultRole(false);
        roleDao.save(role);
        // 添加一个默认权限，重新编辑
        RolePower rolePower = new RolePower();
        rolePower.setRoleId(role.getId());
        rolePower.setPowerId(1);
        rolePowerDao.save(rolePower);
        return role;
    }

    @Override
    public Role updateRole(Role role) {
        Role role1 = roleDao.queryRoleBySign(role.getSign());
        if (role1 != null) {
            if (role.getId() == role1.getId()) {
                roleDao.update(role);
            }else{
                throw new BusinessException("系统标记已存在");
            }
        }
        roleDao.update(role);
        return role;
    }

    @Override
    public boolean setDefaultRole(Role role) {
        Role r = roleDao.queryRoleByDefault();
        if (r != null) {
            r.setDefaultRole(false);
            roleDao.update(r);
        }
        Role role1 = roleDao.queryById(role.getId());
        if (role1.getDefaultRole()) {
            role1.setDefaultRole(false);
        } else {
            role1.setDefaultRole(true);
        }
        role1.setUpdateTime(new Date());
        roleDao.update(role1);
        return true;
    }

    @Override
    public boolean updateRolePower(Integer roleId, Integer[] powerIds) {
        if (powerIds.length > 0) {
            List<RolePower> rpList = rolePowerDao.queryRolePowerByRoleId(roleId);
            if (rpList.size() > 0) {
                for (RolePower rp : rpList) {
                    rolePowerDao.delete(rp.getId());
                }
            }
            for (int i = 0; i < powerIds.length; i++) {
                RolePower rolePower = new RolePower();
                rolePower.setRoleId(roleId);
                rolePower.setPowerId(powerIds[i]);
                rolePowerDao.save(rolePower);
            }
        }
        return true;
    }

}
